package cn.edu.cjw.APMFP.ui.dialog;

import java.awt.*;
import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.swing.*;

import cn.edu.cjw.APMFP.RoleRight.Service.RoleRightService;
import cn.edu.cjw.APMFP.RoleRight.Service.RrServiceImpl;
import cn.edu.cjw.APMFP.RoleRight.pojo.RoleRight;
import cn.edu.cjw.APMFP.Util.StringUtil;
import cn.edu.cjw.APMFP.enums.RoleRightEnums;
import cn.edu.cjw.APMFP.ui.component.combobox.MultiComboBox;
import cn.edu.cjw.APMFP.ui.table.TableCallBack;


public class RoleDialog extends JDialog {
	
	private JButton confirmBtn = new JButton("确认");
	private JButton cancelBtn = new JButton("取消");
	
	private RoleRightService service = new RrServiceImpl();
	
	private RoleRight entity;
	
	private TableCallBack callBack;
	
	public RoleDialog(RoleRight entity, TableCallBack callBack){
		this.entity = entity;
		this.callBack = callBack;
		this.setTitle(entity != null ? "编辑" : "新增");
		this.setSize(500, 500);
		this.init();
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setLocationRelativeTo(null);
//		this.pack();
		this.setVisible(true);
	}

	private void init(){
		this.setLayout(new BorderLayout());

		GridBagLayout gridBagLayout = new GridBagLayout();
		JPanel centerPanel = new JPanel(gridBagLayout);
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.insets = new Insets(10, 10, 10, 10);
		constraints.ipady = 10;

		JLabel idLabel = new JLabel("编号:");
		JTextField idField = new JTextField(27);
		constraints.gridx = 0;
		constraints.gridy = 0;
		centerPanel.add(idLabel, constraints);
		constraints.gridx = 1;
		centerPanel.add(idField, constraints);

		JLabel nameLabel = new JLabel("名称:");
		JTextField nameField = new JTextField(27);
		constraints.gridx = 0;
		constraints.gridy = 1;
		centerPanel.add(nameLabel, constraints);
		constraints.gridx = 1;
		centerPanel.add(nameField, constraints);

		JLabel rightLabel = new JLabel("权限:");
		MultiComboBox rightField = new MultiComboBox(RoleRightEnums.getRightList().toArray());
		rightField.setPreferredSize(nameField.getPreferredSize());
		constraints.gridx = 0;
		constraints.gridy = 2;
		centerPanel.add(rightLabel, constraints);
		constraints.gridx = 1;
		centerPanel.add(rightField, constraints);

		JLabel descLabel = new JLabel("备注:");
		JTextArea descField = new JTextArea(3, 27);
		constraints.gridx = 0;
		constraints.gridy = 3;
		centerPanel.add(descLabel, constraints);
		constraints.gridx = 1;
		centerPanel.add(descField, constraints);

		if(entity != null){
			idField.setEditable(false);
			idField.setText(entity.getRNum());
			nameField.setText(entity.getRName());
			rightField.setSelectValues(entity.getRRight().toArray());
			descField.setText(entity.getRDescribe());
		}

		this.add(centerPanel, BorderLayout.CENTER);

		JPanel footerPanel = new JPanel();
		footerPanel.setPreferredSize(new Dimension(this.getWidth(), 100));
		footerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 10));
		footerPanel.add(confirmBtn);
		footerPanel.add(cancelBtn);
		confirmBtn.addActionListener(e -> {
			String id = idField.getText().trim();
			if(!StringUtil.isNotBlank(id)){
				JOptionPane.showMessageDialog(null, "编号不能为空");
				return;
			}
			String name = nameField.getText().trim();
			if(!StringUtil.isNotBlank(name)){
				JOptionPane.showMessageDialog(null, "名称不能为空");
				return;
			}
			Object[] rightArr = rightField.getSelectedValues();
			if(rightArr.length == 0){
				JOptionPane.showMessageDialog(null, "请选择权限");
				return;
			}
			String desc = descField.getText().trim();
			if(!StringUtil.isNotBlank(desc)){
				JOptionPane.showMessageDialog(null, "描述不能为空");
				return;
			}
			try {
				if(entity == null){
					RoleRight r = new RoleRight();
					r.setRNum(id);
					r.setRName(name);
					r.setRRight(new ArrayList<>(Stream.of(rightArr).map(Object::toString).collect(Collectors.toList())));
					r.setRDescribe(desc);
					service.addRole(r);
				}else{
					service.updateRole(entity, new ArrayList<>(Stream.of(rightArr).map(Object::toString).collect(Collectors.toList()))
							, desc, name);
				}
			} catch (Exception ex) {
				JOptionPane.showMessageDialog(null, "操作失败, 原因:" + ex.getMessage());
				return;
			}
			if(callBack != null){
				callBack.refreshTable();
			}
			JOptionPane.showMessageDialog(null, "操作成功");
			close();
		});
		cancelBtn.addActionListener(e -> {
			close();
		});
		this.add(footerPanel, BorderLayout.SOUTH);
	}
	
	private void close(){
		this.dispose();
	}
	
	public static void main(String[] args) {
		new RoleDialog(null, null);
	}

}
